package com.zoshine.zzg.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.zoshine.zzg.base.BaseResponse;
import com.zoshine.zzg.exception.AppException;
import com.zoshine.zzg.exception.ErrorMessage;
import com.zoshine.zzg.pojo.*;
import com.zoshine.zzg.pojo.pojostatus.InvoiceStatus;
import com.zoshine.zzg.respons.AppResponse;
import com.zoshine.zzg.service.InvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**发货单Controller
 * @author 田沛凌
 * @time 2017/7/24 11:07
 */
@RestController
@RequestMapping(value = "invoice",method = RequestMethod.POST)
public class InvoiceController extends BaseController{

    private InvoiceService invoiceService;
    @Autowired
    public void setInvoiceService(InvoiceService invoiceService) {
        this.invoiceService = invoiceService;
    }
    /**
     * 新增发货单
     * @param idAndCounts 待发货单ID和数量
     */
    @RequestMapping("/addInvoice")
    public BaseResponse addInvoice(String idAndCounts, HttpSession session){
        List<IdAndCount> list = JSON.parseArray(idAndCounts, IdAndCount.class);
        invoiceService.addInvoice(list,session);
        return AppResponse.OK;
    }

    /**
     * 查询发货单集合(分页)
     * @param invoice 发货单
     * @return 发货单集合
     */
    @RequestMapping("/pageInvoice")
    public BaseResponse pageInvoice(Invoice invoice){
        List<Long> supplierPower = super.getSupplierPower();
        List<Long> prisonPower = super.getPrisonPower();
        invoice.setSupplierPower(supplierPower);
        invoice.setPrisonPower(prisonPower);
        List<Invoice> list = invoiceService.list(invoice);
        return new AppResponse(new PageInfo<>(list));
    }
    /**
     * 查询发货单集合
     * @param invoice 发货单
     * @return 发货单集合
     */
    @RequestMapping("/listInvoice")
    public BaseResponse listInvoice(Invoice invoice){
        List<Long> supplierPower = super.getSupplierPower();
        List<Long> prisonPower = super.getPrisonPower();
        invoice.setSupplierPower(supplierPower);
        invoice.setPrisonPower(prisonPower);
        List<Invoice> list = invoiceService.list(invoice);
        return new AppResponse(list);
    }

    /**
     * 获取单个发货单详情
     * @param id 详情ID
     * @return 发货单详情
     */
    @RequestMapping("/getInvoiceDetail")
    public BaseResponse getInvoiceDetail(Long id){
        InvoiceDetail invoiceDetailById = invoiceService.getInvoiceDetailById(id);
        return new AppResponse(invoiceDetailById);
    }
    /**
     * 根据发货单查询详情
     * @param id 发货单号
     * @return 发货详情
     */
    @RequestMapping("/listDetail")
    public BaseResponse listDetail(Long id){
        List<InvoiceDetail> invoiceDetails = invoiceService.listInvoiceDetail(id);
        return new AppResponse(invoiceDetails);
    }

    /**
     * 批量删除发货单
     * @param delIdStr 发货单ID集合
     */
    @RequestMapping("/delInvoice")
    public BaseResponse delInvoice(String delIdStr){
        invoiceService.delInvoice(delIdStr);
        return  AppResponse.OK;
    }


    /**
     * 删除发货单
     * @param id 发货单ID
     */
    @RequestMapping("/delInvoiceById")
    public BaseResponse delInvoiceById(Long id){
        invoiceService.delInvoiceById(id);
        return  AppResponse.OK;
    }

    /**
     * 删除发货单详情
     * @param id 发货单详情ID
     */
    @RequestMapping("/delInvoiceDetail")
    public BaseResponse delInvoiceDetail(Long id){
        invoiceService.delInvoiceDetail(id);
        return  AppResponse.OK;
    }
    /**
     * 修改发货单详情
     * @param invoiceDetail 发货单详情
     */
    @RequestMapping("/editInvoiceDetail")
    public BaseResponse editInvoiceDetail(InvoiceDetail invoiceDetail){
        invoiceService.editInvoiceDetail(invoiceDetail);
        return  AppResponse.OK;
    }

    /**
     * 确认发货
     * @param idStr 发货单ID集合
     */
    @RequestMapping("/passInvoice")
    public BaseResponse passInvoice(String idStr){
        invoiceService.editInvoiceStatus(idStr, InvoiceStatus.FINISH.getValue());
        return AppResponse.OK;
    }

    /**
     * 确认收货
     * @param idStr 发货单ID集合
     */
    @RequestMapping("/confirmInvoice")
    public BaseResponse confirmInvoice(String idStr){
        invoiceService.confirmInvoice(idStr, InvoiceStatus.CONFIRM.getValue());
        return AppResponse.OK;
    }
    /**
     * 生成结算单
     * @param idStr 发货单ID集合
     */
    @RequestMapping("/settlement")
    public BaseResponse settlement(String idStr){
        //为空判断
        if (idStr == null || idStr.length()==0) {
            throw new AppException(ErrorMessage.REQUEST_PARAM_CAN_NOT_NULL);
        }
        String[] pidArr = idStr.split(",");
        List<Invoice> invoices=new ArrayList<>();
        for (String id : pidArr) {
            //判断是否是已收货状态
            Invoice invoice = invoiceService.get(Long.parseLong(id));
            if(!(invoice.getStatus().equals(InvoiceStatus.CONFIRM.getValue()))){
                throw new AppException(ErrorMessage.STATUS_SHOULD_BE_CONFIRM);
            }
            //添加创建人
            invoice.setCreateUser(super.getLoginUserId());
            invoices.add(invoice);
        }
        if(invoices.size()>0){
            invoiceService.settlement(invoices, InvoiceStatus.SETTLEMENT.getValue());
        }
        return AppResponse.OK;
    }

    /**
     * 根据发货单查询犯人信息
     * @param idStr 发货单ID
     * @return 犯人信息
     */
    @RequestMapping("/getPrisonerInfoByInvoiceId")
    public BaseResponse getPrisonerInfoByInvoiceId(Long idStr){

        List<PrisonerInfo> prisonerInfos=invoiceService.getPrisonerInfoByInvoiceId(idStr);

        return new AppResponse(prisonerInfos);
    }

    /**Register for out of stock
     * 根据犯人ID和发货单ID获取商品信息
     * @return 商品信息
     */
    @RequestMapping("/getGoodsHistoryDetail")
    public BaseResponse getGoodsHistoryDetail(String goodsReturn){
        GoodsReturn temp=JSON.parseObject(goodsReturn,GoodsReturn.class);
        List<GoodsHistoryDetail> goodsPublishDetails=invoiceService.getGoodsHistoryDetail(temp.getInvoiceId(),temp.getPrisonerId());
        return  new AppResponse(goodsPublishDetails);
    }
}
